package com.wgchao.amc.mapstruct.mapper.expand;

import com.wgchao.amc.entity.base.SysBaseGroup;
import com.wgchao.amc.entity.base.SysBaseRole;
import com.wgchao.amc.entity.base.SysBaseUr;
import com.wgchao.amc.mapstruct.DTOMapper;
import com.wgchao.amc.mapstruct.dto.SysBaseUrDTO;
import com.wgchao.amc.mapstruct.mapper.SysBaseUrMapper;
import com.wgchao.amc.repository.base.SysBaseGroupRepository;
import com.wgchao.amc.repository.base.SysBaseRoleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;

/**
 * @ClassName: SysBaseUrMapperExpand
 * @author: wgchao
 * @createTime: 2019/11/8 10:09 AM
 * @Description: TODO
 */
@Component
public class SysBaseUrMapperExpand implements DTOMapper<SysBaseUr, SysBaseUrDTO> {
    @Autowired
    private SysBaseUrMapper urMapper;
    @Autowired
    private SysBaseRoleRepository roleRepository;
    @Autowired
    private SysBaseRoleMapperExpand roleMapperExpand;
    @Autowired
    private SysBaseGroupRepository groupRepository;
    @Autowired
    private SysBaseGroupMapperExpand groupMapperExpand;

    @Override
    public SysBaseUr toEntity(SysBaseUrDTO sysBaseUrDTO) {
        return urMapper.toEntity(sysBaseUrDTO);
    }

    @Override
    public SysBaseUrDTO toDTO(SysBaseUr sysBaseUr) {
        return urMapper.toDTO(sysBaseUr);
    }

    @Override
    public List<SysBaseUr> toEntity(List<SysBaseUrDTO> sysBaseUrDTOS) {
        return urMapper.toEntity(sysBaseUrDTOS);
    }

    @Override
    public List<SysBaseUrDTO> toDTO(List<SysBaseUr> sysBaseUrs) {
        return urMapper.toDTO(sysBaseUrs);
    }

    public SysBaseUrDTO getDetail(SysBaseUrDTO urDTO){
        if(urDTO.getGroupId() != null){
            Optional<SysBaseGroup> groupOptional = groupRepository.findById(urDTO.getGroupId());
            if(groupOptional.isPresent()){
                urDTO.setGroupInfo(groupMapperExpand.toDTO(groupOptional.get()));
            }
        }
        if(urDTO.getRoleId() != null){
            Optional<SysBaseRole> roleOptional = roleRepository.findById(urDTO.getRoleId());
            if(roleOptional.isPresent()){
                urDTO.setRoleInfo(roleMapperExpand.toDTO(roleOptional.get()));
            }
        }
        return urDTO;
    }
}
